//
//  StepView.m
//  LineByLine
//
//  Created by 병철 김 on 11. 11. 12..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "StepView.h"
#import "CommonDefine.h"


@implementation StepView
@synthesize tableView, curPage, dataSource;
@dynamic viewDelegate;

- (UITableViewCell*) getCurrentCell {
    NSArray *cells = [tableView visibleCells];
    if (cells != nil && [cells count] > 0) {
        return [cells objectAtIndex:0];
    }
    return nil;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [tableView setRowHeight:frame.size.height];
        [tableView setPagingEnabled:YES];
        [tableView setShowsVerticalScrollIndicator:NO];
        [tableView setShowsHorizontalScrollIndicator:NO];
        [tableView setDirectionalLockEnabled:YES];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [tableView setBackgroundColor:[UIColor clearColor]];
        [tableView setDelegate:self];
        
        [self addSubview:tableView];
        [tableView release];
        
        UIImageView *pagesImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pages.png"]];
        [pagesImageView setFrame:CGRectMake(10, 5, 24, 31)];
        [self addSubview:pagesImageView];
        [pagesImageView release];
        
        bookmarkImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bookmark.png"]];
        [bookmarkImageView setUserInteractionEnabled:YES];
        [bookmarkImageView setFrame:CGRectMake(kContentWidth - 46, 0, 36, 51)];
        [bookmarkImageView setContentStretch:CGRectMake(0, 0.1, 1, 0)];
        [self addSubview:bookmarkImageView];
        [bookmarkImageView release];
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBookmark)];
        [bookmarkImageView addGestureRecognizer:gestureRecognizer];
        [gestureRecognizer release];
    }
    return self;
}

- (void) didChangePage {
    UITableViewCell *cell = [self getCurrentCell];
    id sentence = [cell valueForKey:@"sentence"];
    int sid = [[sentence valueForKey:@"sid"] intValue];
    
    if (sid != curPage) {
        curPage = sid;
        [self.viewDelegate didChangePage:curPage];
    }
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self didChangePage];
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    [self didChangePage];
}

- (void) configView {
    [tableView setDataSource:dataSource];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(curPage-1) inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

- (void) onClickBookmark {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(onClickBookmark2)];
    [UIView setAnimationDuration:0.4];
    CGRect frame = [bookmarkImageView frame];
    frame.size.height = frame.size.height + 50;
    [bookmarkImageView setFrame:frame];
    [UIView commitAnimations];
}

- (void) onClickBookmark2 {
    if (self.viewDelegate) {
        [self.viewDelegate onClickBookmark];
    }
}

- (BOOL) moveNext {
    int rows = [dataSource tableView:tableView numberOfRowsInSection:0];
    if (curPage < rows) {
        [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(curPage) inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
        [self didChangePage];
        return YES;
    } else {
        return NO;
    }
}
@end
